package me.zhengjie.modules.app.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.app.domain.GameRecords;
import me.zhengjie.modules.app.domain.dto.GameRecordsDto;
import me.zhengjie.modules.app.domain.vo.GameRecordsVo;
import me.zhengjie.modules.app.mapstruct.GameRecordsMapStruct;
import me.zhengjie.modules.app.service.GameRecordsService;
import me.zhengjie.modules.security.service.dto.JwtUserDto;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import net.dreamlu.mica.core.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Slf4j
@Api("app/api/v1/game-records")
@RestController
@RequestMapping("/app/api/v1/game-records")
public class GameRecordsController {
    @Autowired
    private GameRecordsService gameRecordsService;
    @Autowired
    private GameRecordsMapStruct gameRecordsMapStruct;
    /**
     * 用户开局
     * @param dto
     * @return
     */
    @PostMapping("/opening")
    public ResponseEntity opening(@RequestBody GameRecordsDto dto){
        if(null == dto.getPlayer1Id() || null == dto.getPlayer2Id()){
            throw new BadRequestException("请设置对局用户");
        }
        if(StringUtils.isEmpty(dto.getConfigSnapshot())){
            throw new BadRequestException("请上传开局手牌照片");
        }
        GameRecords gameRecords = new GameRecords();
        gameRecords.setId(UUID.randomUUID().toString());
        gameRecords.setPlayer1Id(dto.getPlayer1Id());
        gameRecords.setPlayer2Id(dto.getPlayer2Id());
        gameRecords.setConfigSnapshot(dto.getConfigSnapshot());
        int num = gameRecordsService.insertData(gameRecords);
        if(num > 0){
            return ResponseEntity.ok(gameRecords);
        }
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/page")
    public R page( Page page,  GameRecordsVo vo){
        QueryWrapper<GameRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("player1_id", vo.getPlayer1Id() );
        Page pageInfo = gameRecordsService.page(page,queryWrapper);

        return R.success(pageInfo);
    }
}
